/*
 *  Copyright 2007 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.backup;

import com.blandware.atleap.webapp.action.core.BaseAction;
import com.blandware.atleap.webapp.util.core.*;
import com.blandware.atleap.webapp.menu.MenuRepository;
import com.blandware.atleap.webapp.acegi.UserManagerDaoImpl;
import com.blandware.atleap.service.core.ContentLocaleManager;
import com.blandware.atleap.service.core.MailTemplateManager;
import com.blandware.atleap.common.Constants;
import com.blandware.atleap.search.SearchManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;

/**
 * Contains support for restore actions.
 * <p><a href="RestoreActionSupport.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.1 $ $Date: 2007/06/06 13:43:02 $
 */
public abstract class RestoreActionSupport extends BaseAction {
    /**
     * Refreshes everything that depends on DB after its state has bean restored.
     *
     * @param request request currently being processed
     */
    protected void doAfterRestore(HttpServletRequest request) {
        ServletContext servletContext = getServlet().getServletContext();

        //load content locales into cache
        ContentLocaleManager contentLocaleManager = (ContentLocaleManager)getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN);
        contentLocaleManager.initialize();
        LocaleUtil.getInstance(servletContext).synchronizeCache();

        //initialize menu repository and replace it in the servlet context
        MenuRepository menuRepository = new MenuUtil(request).initializeMenuRepository();
        servletContext.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, menuRepository);

        //global properties
        GlobalProperties.getInstance(servletContext).synchronizeCache();

        //mail templates
        MailTemplateManager mailTemplateManager = (MailTemplateManager) getBean(Constants.MAIL_TEMPLATE_MANAGER_BEAN);
        mailTemplateManager.initialize();

        // initialize application resources
        ApplicationResources.getInstance(servletContext).loadResources();

        // flush all cache
        CacheUtil.getInstance(request).flushAllCache();

        // reindex all entities
        SearchManager.getInstance(servlet.getServletContext()).reIndexAll(request);

        // refresh tiles config
        TileContainerUtil.getInstance(servlet.getServletContext()).synchronizePersistedDefinitions(false);

        // update current user permissions
        if (request.getRemoteUser() != null) {
            UserManagerDaoImpl userManagerDaoImpl = (UserManagerDaoImpl) getBean(Constants.USER_DETAILS_SERVICE_BEAN);
        	userManagerDaoImpl.updateUser(request.getRemoteUser());
        }
    }
}
